﻿@page "/gateway/global"

@inject IApiGateway iApiGateway



<Form Model="@gatewayGlobalOption"
      LabelColSpan="8"
      WrapperColSpan="16"
      OnFinish="OnFinish"
      Size="@AntSizeLDSType.Default">


    <Tabs DefaultActiveKey="1" @bind-ActiveKey="@nKey">
        <TabPane Key="1" Tab="负载均衡">
            <FormItem Label="均衡类型">
                <RadioGroup @bind-Value="@context.LoadBalancerOptions.Type">
                    <Radio RadioButton Value="@(LoadBalancerEnum.RoundRobin)">轮询</Radio>
                    <Radio RadioButton Value="@(LoadBalancerEnum.LeastConnection)">最少负担</Radio>
                    <Radio RadioButton Value="@(LoadBalancerEnum.NoLoadBalancer)">第一个可用</Radio>
                </RadioGroup>
            </FormItem>
        </TabPane>

        <TabPane Key="2" Tab="限流扩展配置">

            <FormItem Label="过载警告信息">
                <Input @bind-Value="@context.RateLimitOptions.QuotaExceededMessage" />
            </FormItem>
            <FormItem Label="HttpStatusCode">
                <Input @bind-Value="@context.RateLimitOptions.HttpStatusCode" />
            </FormItem>
        </TabPane>

        <TabPane Key="3" Tab="服务质量与熔断">
            <FormItem Label="允许故障数量">
                <Input @bind-Value="@context.QoSOptions.ExceptionsAllowedBeforeBreaking" />
            </FormItem>
            <FormItem Label="熔断时间">
                <AntDesign.Input @bind-Value="@context.QoSOptions.DurationOfBreak">
                    <AddOnAfter>ms</AddOnAfter>
                </AntDesign.Input>
            </FormItem>
            <FormItem Label="请求超时">
                <AntDesign.Input @bind-Value="@context.QoSOptions.TimeoutValue">
                    <AddOnAfter>ms</AddOnAfter>
                </AntDesign.Input>
            </FormItem>

        </TabPane>

    </Tabs>

    <FormItem WrapperColOffset="8" WrapperColSpan="16">
        <Button HtmlType="submit">
            Submit
        </Button>
    </FormItem>
</Form>

<style>
    .ant-form-item-label {
        flex: 0 0 100px !important;
    }

    .ant-col-offset-8 {
        margin-left: 100px;
    }
</style>

@code
{


    private GatewayGlobalOption gatewayGlobalOption = new GatewayGlobalOption
    {
        LoadBalancerOptions = new LoadBalancerOptions(),
        RateLimitOptions = new RateLimitOptions(),
        QoSOptions = new QoSOptions()
    };

    WindowsHelper windows;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            windows = new WindowsHelper(jSRuntime);
            await windows.SetTitleAsync("Gateway Global Config");
            await InvokeAsync(async () => {
                var global = await this.iApiGateway.GetGlobalConfig();
                if (global != null)
                {
                    this.gatewayGlobalOption = global.RouteConfig.TryToEntity<GatewayGlobalOption>();
                }
                base.StateHasChanged();
            });
            await windows.HideLoadingAsync();
            await windows.SetTitleAsync("Gateway Global Edit");
        }

    }

    string nKey { get; set; } = "1";

    private async void OnFinish(EditContext editContext)
    {
        if (await iApiGateway.AddOrUpdateGlobalConfig(this.gatewayGlobalOption))
        {
            var task = notice.Success(new NotificationConfig {
                Message = "保存成功"
            });
        }
        else
        {
            var task = notice.Error(new NotificationConfig
            {
                Message = "操作失败请重试"
            });
        }
        await windows.ConsoleErrorAsync(this.gatewayGlobalOption);
    }

}
